package org.budo.support.spring.context.aware;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import lombok.extern.slf4j.Slf4j;

/**
 * @author lmw
 */
@Slf4j
public abstract class RootApplicationContextRefreshedEventListener implements ApplicationListener<ApplicationEvent> {
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (!(event instanceof ContextRefreshedEvent)) {
            return;
        }

        ContextRefreshedEvent contextRefreshedEvent = (ContextRefreshedEvent) event;
        ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
        ApplicationContext parent = applicationContext.getParent();

        log.info("#27 event=" + event + ", applicationContext=" + applicationContext + ", parent=" + parent);
        if (null != parent) {
            return;
        }

        this.onRootApplicationContextRefreshedEvent(contextRefreshedEvent);
    }

    protected abstract void onRootApplicationContextRefreshedEvent(ContextRefreshedEvent contextRefreshedEvent);
}